package cn.fzkj.ailangchain4j.mcp;

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.http.StreamableHttpMcpTransport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @ description
 * @ author yaya
 * @ since 2025/11/4
 */
@Slf4j
//@Configuration
public class McpConfig {

    @Value("${bigmodel.api-key}")
    private String apiKey;

//    @Bean
    public McpToolProvider mcpToolProvider() {
        McpTransport transport = new StreamableHttpMcpTransport.Builder()
                .url("https://open.bigmodel.cn/api/paas/v4/web_search")
                .customHeaders(Map.of("Authorization", "Bearer " + apiKey,
                        "Content-Type", "application/json",
                        "search_engine", "search_pro_sogou",
                        "search_intent", "false"))
                .logRequests(true)
                .logResponses(true)
                .build();

        McpClient mcpClient = new DefaultMcpClient.Builder()
//                .key(apiKey)
                .transport(transport)
                .build();

        McpToolProvider toolProvider = McpToolProvider.builder()
                .mcpClients(List.of(mcpClient))
                .build();

        return toolProvider;
    }
}
